package cn.iocoder.yudao.module.blog.controller.admin.jiucase;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.blog.controller.admin.jiucase.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.jiucase.BlogJiuCaseDO;
import cn.iocoder.yudao.module.blog.convert.jiucase.BlogJiuCaseConvert;
import cn.iocoder.yudao.module.blog.service.jiucase.BlogJiuCaseService;

@Tag(name = "管理后台 - 灸法案例")
@RestController
@RequestMapping("/blog/jiu-case")
@Validated
public class BlogJiuCaseController {

    @Resource
    private BlogJiuCaseService jiuCaseService;

    @PostMapping("/create")
    @Operation(summary = "创建灸法案例")
    @PreAuthorize("@ss.hasPermission('blog:jiu-case:create')")
    public CommonResult<Long> createJiuCase(@Valid @RequestBody BlogJiuCaseCreateReqVO createReqVO) {
        return success(jiuCaseService.createJiuCase(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新灸法案例")
    @PreAuthorize("@ss.hasPermission('blog:jiu-case:update')")
    public CommonResult<Boolean> updateJiuCase(@Valid @RequestBody BlogJiuCaseUpdateReqVO updateReqVO) {
        jiuCaseService.updateJiuCase(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除灸法案例")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blog:jiu-case:delete')")
    public CommonResult<Boolean> deleteJiuCase(@RequestParam("id") Long id) {
        jiuCaseService.deleteJiuCase(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得灸法案例")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blog:jiu-case:query')")
    public CommonResult<BlogJiuCaseRespVO> getJiuCase(@RequestParam("id") Long id) {
        BlogJiuCaseDO jiuCase = jiuCaseService.getJiuCase(id);
        return success(BlogJiuCaseConvert.INSTANCE.convert(jiuCase));
    }

    @GetMapping("/list")
    @Operation(summary = "获得灸法案例列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('blog:jiu-case:query')")
    public CommonResult<List<BlogJiuCaseRespVO>> getJiuCaseList(@RequestParam("ids") Collection<Long> ids) {
        List<BlogJiuCaseDO> list = jiuCaseService.getJiuCaseList(ids);
        return success(BlogJiuCaseConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得灸法案例分页")
    @PreAuthorize("@ss.hasPermission('blog:jiu-case:query')")
    public CommonResult<PageResult<BlogJiuCaseRespVO>> getJiuCasePage(@Valid BlogJiuCasePageReqVO pageVO) {
        PageResult<BlogJiuCaseDO> pageResult = jiuCaseService.getJiuCasePage(pageVO);
        return success(BlogJiuCaseConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出灸法案例 Excel")
    @PreAuthorize("@ss.hasPermission('blog:jiu-case:export')")
    @OperateLog(type = EXPORT)
    public void exportJiuCaseExcel(@Valid BlogJiuCaseExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BlogJiuCaseDO> list = jiuCaseService.getJiuCaseList(exportReqVO);
        // 导出 Excel
        List<BlogJiuCaseExcelVO> datas = BlogJiuCaseConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "灸法案例.xls", "数据", BlogJiuCaseExcelVO.class, datas);
    }

}
